<?php
class eBayRequest extends gravy{
	public $rawxml="";
	public $xml;

	private $devID;
	private $appID;
	private $certID;
	private $serverUrl;
	private $compatLevel;
	private $siteID;
	private $verb;

    public function __construct($verb){
		parent::__construct();
		$this->devID=eBayConfig::DEV_ID;
		$this->appID=eBayConfig::APP_ID;
		$this->certID=eBayConfig::CERT_ID;
		$this->compatLevel=eBayConfig::COMPATABILITY_LEVEL;
		$this->siteID=eBayConfig::SITE_ID;
		$this->verb=$verb;
        $this->serverUrl=eBayConfig::SOAP_GATEWAY;	
	}

	public function fetch($requestBody){
		$result=false;

		//build eBay headers using variables passed via constructor
		$headers=$this->buildHeaders();
		
		//initialise a CURL session
		$connection = curl_init();
		//set the server we are using (could be Sandbox or Production server)
		curl_setopt($connection, CURLOPT_URL, $this->serverUrl);
		
		//stop CURL from verifying the peer's certificate
		curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
		
		//set the headers using the array of headers
		curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
		
		//set method as POST
		curl_setopt($connection, CURLOPT_POST, 1);
		
		//set the XML body of the request
		curl_setopt($connection, CURLOPT_POSTFIELDS, $requestBody);
		
		//set it to return the transfer as a string from curl_exec
		curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
        
		//Send the Request
		$response = curl_exec($connection);
        
        //print $response;
		
		//close the connection
		curl_close($connection);

		if(stristr($response,'HTTP 404')||$response==''){
			$this->addError("eAPIx0001","Error Loading Data","<p>An error was generated while trying to retrieve that information.</p><p>Please try again, or contact support with the following error code:</p>",array("request"=>$requestBody,"response"=>$response));
		}else{
			$this->rawxml=$response;
			if($this->xml=@simplexml_load_string($response)){
				$ack=strtolower((string)$this->xml->Ack);
				if($ack=="success"||$ack=="warning"){
					$result=$this->xml;
				}else{
					$this->addError("eAPIx0002","Error Loading Data","<p>An error was generated while trying to retrieve that information.</p><p>Please try again, or contact support with the following error code:</p>",array("request"=>$requestBody,"response"=>$response));
				}
			}else{
				$this->addError("eAPIx0003","Error Loading Data","<p>An error was generated while trying to retrieve that information.</p><p>Please try again, or contact support with the following error code:</p>",array("request"=>$requestBody,"response"=>$response));
			}
		}

		return($result);
	}
	
	
	
	/**	buildEbayHeaders
		Generates an array of string to be used as the headers for the HTTP request to eBay
		Output:	String Array of Headers applicable for this call
	*/
	private function buildHeaders(){
		$headers = array (
			//Regulates versioning of the XML interface for the API
			'X-EBAY-API-COMPATIBILITY-LEVEL: '.$this->compatLevel,

			//set the keys
			'X-EBAY-API-DEV-NAME: '.$this->devID,
			'X-EBAY-API-APP-NAME: '.$this->appID,
			'X-EBAY-API-CERT-NAME: '.$this->certID,
			
			//the name of the call we are requesting
			'X-EBAY-API-CALL-NAME: '.$this->verb,			
			
			//SiteID must also be set in the Request's XML
			//SiteID = 0  (US) - UK = 3, Canada = 2, Australia = 15, ....
			//SiteID Indicates the eBay site to associate the call with
			'X-EBAY-API-SITEID: '.$this->siteID,
		);
		return($headers);
	}
}
?>